package com.travel.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.travel.Bean.Ticket;
import com.travel.Dao.TicketMapper;

import com.travel.JPA.TicketJPA;
import com.travel.Pojo.TicketAndCultureName;

import com.travel.util.Annotation.Redis;
import com.travel.util.ConsoleOutputControl;
import com.travel.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class TicketServiceImpl {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TicketJPA ticketJPA;
    @Autowired
    private TicketMapper ticketMapper;

    @Value("${travel.RedisCache}")
    static String CacheValue;
    /**
     * 选取项目列表
     *
     * @param ticketName 项目名字
     * @param cultureId  景区id
     * @param page       页面
     * @param limit      记录条数
     * @return {@link PageInfo}<{@link TicketAndCultureName}>
     */

    @Cacheable(value = "Travel_Ticket",key = "(#ifAdmin == true ? 'admin' : 'user' ) (#cultureId == null?0:#cultureId) ")
    public PageInfo<TicketAndCultureName> selTicketList(String ticketName,Integer cultureId,
                                                        Integer page,Integer limit,boolean ifAdmin){
        PageHelper.startPage(page,limit);
        List<TicketAndCultureName> ticketAndCultureNameList;
        if(ifAdmin)
            //查询管理员可见的景区项目列表
             ticketAndCultureNameList = ticketMapper.selTicketList(ticketName,cultureId,"0",null);
        else
            //查询用户可见的景区项目列表
             ticketAndCultureNameList = ticketMapper.selTicketList(ticketName,cultureId,null,"0");

        return new PageInfo<>(ticketAndCultureNameList);
    }

    /**
     * 保存添加项目
     *
     * @param ticket 项目
     * @return {@link Integer}
     */
    @CacheEvict(value = "Travel_Ticket",key = "#admin == null ? #ticket.culture_id : 0")
    public Integer saveAddTicket(Ticket ticket,String admin){
        //清空redis中的用户获取的项目列表缓存
        redisUtil.deleteKey("Travel_Ticket:user" + (admin == null ? String.valueOf(ticket.getCulture_id()) : "0"));
        try{
            ticketJPA.save(ticket);
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("ticketJPA.save 保存错误");
            return 1;
        }
        return 0;
    }


    /**
     * 删除项目——管理员
     *
     * @param admin 管理
     * @param ids   ticket id 数组
     * @return {@link Integer}
     */
    @Transactional
    @CacheEvict(value = "Travel_Ticket",key = "admin (#admin == null ? #cultureId : 0)")
    public Integer deleteTicket(Integer[] ids,String admin,Integer cultureId){
        int res = 0;

        try{
            /*获取删除的条数
            * 使管理员和用户不可见该项目*/
            res = ticketJPA.deleteNoByIds(ids);
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("ticketJPA.delete 删除错误");
            return 1;
        }
//        返回删除的记录条数
        return res;
    }

    /**
     * 隐藏项目用户
     * 隐藏项目——用户
     *
     * @param ticketId  项目id
     * @param cultureId 景区id
     * @return {@link Integer}
     */
    @Transactional
    @CacheEvict(value = "Travel_Ticket",key = "('user') (#cultureId)")
    public Integer hideTicket_User(Integer ticketId,Integer cultureId){
        int res = 0;
        try{
            res = ticketJPA.updateTicketEnable_user(ticketId,1);
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("hideTicket_User error");
            return 0;
        }
        return res;
    }

    /**
     * 复苏项目用户
     * 显示项目——用户
     *
     * @param ticketId  项目id
     * @param cultureId 景区id
     * @return {@link Integer}
     */
    @Transactional
    @CacheEvict(value = "Travel_Ticket",key = "('user') (#cultureId)")
    public Integer recoveryTicket_User(Integer ticketId,Integer cultureId){
        int res = 0;
        try{
            res = ticketJPA.updateTicketEnable_user(ticketId,0);
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("recoveryTicket_User error");
            return 0;
        }
        return res;
    }


    /**
     * 保存编辑项目
     *
     * @param ticket 项目
     * @return {@link Integer}
     */
    @CacheEvict(value = "Travel_Ticket_id",key = "#ticket.getid()")
    public Integer saveEditTicket(Ticket ticket){
        try{
            ticketJPA.save(ticket);
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("ticketJPA.save 保存修改错误");
            return 1;
        }
        return 0;
    }

    /**
     * 得到项目通过id
     *
     * @param ticketId 项目id
     * @return {@link Ticket}
     */
    @Cacheable(value = "Travel_Ticket_id",key = "#ticketId")
    public Ticket getTicketById(Integer ticketId){
        return ticketJPA.findById(ticketId).get();
    }


    /**
     * 得到项目价格
     *
     * @param ticketId 项目id
     * @return {@link Float}
     */
    @Cacheable(value = "Travel_TicketPrice",key = "#ticketId")
    public Float getTicketPrice(Integer ticketId){
        return ticketJPA.findPriceById(ticketId).floatValue();
    }


    /**
     * 得到项目名字
     *
     * @param ticketId 项目id
     * @return {@link String}
     */
    @Cacheable(value = "Travel_TicketName",key = "#ticketId")
    public String getTicketName(Integer ticketId){
        return ticketJPA.findNameById(ticketId);
    }


    /**
     * 找到景区id通过项目id
     *
     * @param ticketId 项目id
     * @return {@link Integer}
     */
    public Integer findCultureIdByTicketId(Integer ticketId){
        return ticketJPA.findCultureIdById(ticketId);
    }

    /**
     * 找到启用用户通过项目id
     *
     * @param ticketId 项目id
     * @return {@link Integer}
     */
    public Integer findEnable_userByTicketId(Integer ticketId){
        return ticketJPA.findEnable_userById(ticketId);
    }

}
